package com.test.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.test.dao.HibernateUtil;
import com.test.util.TaskUtil;

public class ActiveStudentTask implements Task {
	public final static int SLEEP_TIME  = 1800000;
	public final static int SIZE = 10000;
	private boolean[] childComplete = null;
	
	public void execute() {
		synchronized(TaskUtil.activeStudentTaskCount){
			TaskUtil.activeStudentTaskCount++;
		}
		System.out.println(getTaskName()+"任务开始");
		Long beginTime = System.currentTimeMillis(); 
		Connection conn = HibernateUtil.getSessionFactory().openSession().connection();
		String sql = "select count(id) from student where status=? and activeTime is null ";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1, "1");
			ResultSet rs = pst.executeQuery();
			int count = 0;
			if(rs.next()){
				count = rs.getInt(1);
			}
			if(count==0){
				System.out.println(getTaskName()+"本轮没有查询到任何数据，本轮任务结束");
				System.out.println(getTaskName()+"将进行休眠,休眠"+SLEEP_TIME/1000+"秒");
				System.out.println("\n\n\n\n");
				closeJdbcConn(conn,pst,rs);
				Thread.sleep(SLEEP_TIME);
				execute();
			}
			System.out.println(getTaskName()+"本轮查询到"+count+"条数据");
			int round = count/SIZE;
			if(count%SIZE!=0)
				round++;
			
			double num = Math.random()*10;
			if(num>5){
				System.out.println(getTaskName()+"本轮将使用jdbc进行查询修改");
				/* 使用jdbc查询*/
				System.out.println(getTaskName()+"本轮将生成"+round+"个子线程进行操作");
				childComplete = new boolean[round];
				for(int i=0;i<round;i++){
					Thread childtask = new Thread(new ActiveStudentWorkerByjdbc(i,this));
					childtask.start();
				}
				System.out.println(getTaskName()+"任务分配完成，等待子任务完成");
				while(isSuccess()==false){
					System.out.println(getTaskName()+"子任务还未完成，休眠3秒;");
					Thread.sleep(3000);
				}
				Long endTime = System.currentTimeMillis(); 
				System.out.println(getTaskName()+"所有子任务已完成，本轮任务结束，耗时"+(endTime-beginTime)+"毫秒，耗时"+(endTime-beginTime)/1000+"秒;");
				System.out.println(getTaskName()+"将进行休眠,休眠"+SLEEP_TIME/1000+"秒");
				closeJdbcConn(conn,pst,rs);
				Thread.sleep(SLEEP_TIME);
				execute();
			}else{
				System.out.println(getTaskName()+"本轮将使用hibernate进行查询修改");
				System.out.println(getTaskName()+"本轮将生成"+round+"个子线程进行操作");
				childComplete = new boolean[round];
				for(int i=0;i<round;i++){
					Thread childtask = new Thread(new ActiveStudentWorkerByhibernate(i,this));
					childtask.start();
				}
				System.out.println(getTaskName()+"任务分配完成，等待子任务完成");
				while(isSuccess()==false){
					System.out.println(getTaskName()+"子任务还未完成，休眠3秒;");
					Thread.sleep(3000);
				}
				Long endTime = System.currentTimeMillis(); 
				System.out.println(getTaskName()+"所有子任务已完成，本轮任务结束，耗时"+(endTime-beginTime)+"毫秒，耗时"+(endTime-beginTime)/1000+"秒;");
				System.out.println(getTaskName()+"将进行休眠,休眠"+SLEEP_TIME/1000+"秒");
				System.out.println("\n\n\n\n");
				Thread.sleep(SLEEP_TIME);
				execute();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public String getTaskName() {
		return "第"+TaskUtil.activeStudentTaskCount+"轮：Task[ActiveStudentTask]";
	}
	
	

	public void run() {
		execute();
	}
	
	/**
	 * 判断子任务是否完成
	 * @return
	 */
	public boolean isSuccess(){
		for(int i=0;i<this.childComplete.length;i++){
			if(this.childComplete[i])
				continue;
			else
				return false;
		}
		return true;
	}
	
	/**
	 * 子任务完成回调
	 * @param childRound
	 */
	public synchronized void childCallBack(int childRound){
		try{
			if(!this.childComplete[childRound]){
				this.childComplete[childRound]=true;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	/**
	 * 关闭jdbc链接
	 * @param conn
	 * @param s
	 * @param rs
	 * @throws SQLException
	 */
	public void closeJdbcConn(Connection conn,Statement s, ResultSet rs) throws SQLException{
		if(rs!=null)
			rs.close();
		if(s!=null)
			s.close();
		if(conn!=null)
			conn.close();
	}

}
